import time

from common.Utils import BlogDriver
from selenium.webdriver.common.by import By

#博客首页测试用例
class BlogList:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #测试首页（登录情况下）
    def ListTestByLogin(self):
        self.driver.implicitly_wait(3)
        #测试个人信息（头像、昵称、分类、文章）是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > img")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        #测试博客列表是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.date")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.desc")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a")
        #测试主页、写博客、注销按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)")

        #添加屏幕截图
        # BlogDriver.getScreeShot()

        #测试点击主页、写博客跳转的界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")

        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit")

        self.driver.back()#跳转到上一个界面
        #测试注销按钮后跳转的页面，并且将状态设置为未登录
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")

        username = self.driver.find_element(By.CSS_SELECTOR, "#username").get_attribute("value")
        password = self.driver.find_element(By.CSS_SELECTOR, "#password").get_attribute("value")

        assert username == ''
        assert password == ''

    # 测试首页（未登录情况下）
    def ListTestByFailLogin(self):
        # 画面会闪现一下，添加隐式等待
        self.driver.implicitly_wait(2)

        #查找登录元素
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")

# list = BlogList()
# list.ListTestByLogin()

